package com.example.dbcms.service.impl;

import com.example.dbcms.domain.*;
import com.example.dbcms.mapper.FeeStatusDao;
import com.example.dbcms.mapper.ParkingDao;
import com.example.dbcms.mapper.ParkingFeeInfoDao;
import com.example.dbcms.mapper.ResidentDao;
import com.example.dbcms.service.ParkingFeeService;
import com.example.dbcms.utils.DateUtil;
import com.example.dbcms.utils.ToolUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ParkingFeeServiceImpl implements ParkingFeeService {

    @Autowired
    private ParkingFeeInfoDao parkingFeeInfoDao;

    @Autowired
    private ParkingDao parkingDao;

    @Autowired
    private ResidentDao residentDao;

    @Autowired
    private FeeStatusDao feeStatusDao;

    @Override
    public PageInfo<ParkingFeeInfo> selectAllParkingFeeInfo(ParkingFeeInfo parkingFeeInfo) {
        PageHelper.startPage(parkingFeeInfo.getPageNum(), 10);
        List<ParkingFeeInfo> parkingFeeInfoList = parkingFeeInfoDao.selectAllParkingFeeInfo(parkingFeeInfo);
        PageInfo<ParkingFeeInfo> parkingFeeInfoPageInfo = new PageInfo<>(parkingFeeInfoList);
        return parkingFeeInfoPageInfo;
    }

    @Override
    public PageInfo<ParkingFeeInfo> selectAllParkingFeeByParkingId(ParkingFeeInfo parkingFeeInfo) {
        List<Integer> parkingIdByUserId = parkingDao.getParkingIdByUserId(100);
        PageHelper.startPage(parkingFeeInfo.getPageNum(), 10);
        List<ParkingFeeInfo> parkingFeeInfoList = parkingFeeInfoDao.selectAllParkingFeeByParkingId(parkingIdByUserId,parkingFeeInfo);
        PageInfo<ParkingFeeInfo> parkingFeeInfoPageInfo = new PageInfo<>(parkingFeeInfoList);
        return parkingFeeInfoPageInfo;
    }

    @Override
    public int addParkingFee(ParkingFeeInfo parkingFeeInfo) {
        parkingFeeInfo.setId(parkingFeeInfoDao.getMaxId()+1);
        parkingFeeInfo.setParkingNo(parkingDao.getParkingIdByName(parkingFeeInfo.getParkingName()));
        if(parkingFeeInfo.getWhether().equals("是")){
            parkingFeeInfo.setFeeDate(DateUtil.getCurrentDate());
            parkingFeeInfo.setOdd(ToolUtil.getRandomString(20));
        }
        return parkingFeeInfoDao.addParkingFee(parkingFeeInfo);
    }

    @Override
    public int addParkingFee(int parkingId, int year, int month, String fee) {
        ParkingFeeInfo parkingFeeInfo = new ParkingFeeInfo();
        parkingFeeInfo.setId(parkingFeeInfoDao.getMaxId()+1);
        parkingFeeInfo.setParkingNo(parkingId);
        parkingFeeInfo.setYear(year);
        parkingFeeInfo.setMonth(month);
        parkingFeeInfo.setParkingFee(fee);
        parkingFeeInfo.setWhether("否");
        return parkingFeeInfoDao.addParkingFee(parkingFeeInfo);
    }

    @Override
    public ParkingFeeInfo getParkingFee(int id) {
        return parkingFeeInfoDao.getParkingFee(id);
    }

    @Override
    public int editParkingFee(ParkingFeeInfo parkingFeeInfo) {
        parkingFeeInfo.setOdd(ToolUtil.getRandomString(20));
        return parkingFeeInfoDao.editParkingFee(parkingFeeInfo);
    }

    @Override
    public int deleteParkingFee(int id) {
        return parkingFeeInfoDao.deleteParkingFee(id);
    }

    @Override
    public int deleteMultipleParkingFee(List<Integer> ids) {
        return parkingFeeInfoDao.deleteMultipleParkingFee(ids);
    }

    @Override
    public int payParkingFee(int id,String username) {
        ParkingFeeInfo parkingFeeInfo = getParkingFee(id);
        if(parkingFeeInfo.getWhether().equals("否")){
            parkingFeeInfo.setWhether("是");
            parkingFeeInfo.setFeeDate(DateUtil.getCurrentDate());
            parkingFeeInfo.setPeoName(username);
            parkingFeeInfo.setOdd(ToolUtil.getRandomString(20));
            return parkingFeeInfoDao.updateParkingFee(parkingFeeInfo);
        }
        return -1;
    }

    @Override
    public int payMultipleParkingFee(List<Integer> ids, String username) {
        for (int i=0;i<ids.size();i++){
            ParkingFeeInfo parkingFeeInfo = getParkingFee(ids.get(i));
            if(parkingFeeInfo.getWhether().equals("否")){
                parkingFeeInfo.setWhether("是");
                parkingFeeInfo.setFeeDate(DateUtil.getCurrentDate());
                parkingFeeInfo.setPeoName(username);
                parkingFeeInfo.setOdd(ToolUtil.getRandomString(20));
                parkingFeeInfoDao.updateParkingFee(parkingFeeInfo);
            }
        }
        return ids.size();
    }

    @Override
    public CostStatistic getAllParkingFee(CostStatistic costStatistic) {
        CostStatistic costStatistic1 = new CostStatistic();
        if(costStatistic.getBuildingNo()==null||costStatistic.getBuildingNo().length()==0){
            costStatistic.setBuildingNo(null);
        }

        if(costStatistic.getRoomNo()==null||costStatistic.getRoomNo().length()==0){
            costStatistic.setRoomNo(null);
        }

        if (costStatistic.getStartDate()==null||costStatistic.getStartDate().length()==0){
            costStatistic.setStartDate(null);
        }else{
            if(!DateUtil.isValidDate(costStatistic.getStartDate())) {
                costStatistic.setStartDate(null);
            }
        }
        if (costStatistic.getEndDate()==null||costStatistic.getEndDate().length()==0){
            costStatistic.setEndDate(null);
        }else {
            if (!DateUtil.isValidDate(costStatistic.getEndDate())) {
                costStatistic.setStartDate(null);
            }
        }
        int sum = 0; //费用总和
        int sumFeed = 0;  //已缴费总和
        int sumNotFeed = 0;  //未缴费总
        //先对日期田间进行查询
        List<Integer> residentIdList = residentDao.getResidentIdList(costStatistic.getBuildingNo(), costStatistic.getRoomNo());
        List<Integer> parkingIdByUserIdList;//保存查询条件的用户的停车位编号列表
        if(residentIdList.size()!=0){
             parkingIdByUserIdList = parkingDao.getParkingIdByUserIdList(residentIdList);//假设能查到
             if(parkingIdByUserIdList.size()==0){
                /**
                 * 没有该用户(用户组)的停车位费用记录
                 */
                costStatistic1.setParkingFeeValue("0","0","0");
                return costStatistic1;
            }
        }else {
            costStatistic1.setParkingFeeValue("0","0","0");
            /**
             * 没有居民信息,直接不用谈及停车费用了
             */
            return costStatistic1;
        }
        //System.out.println(parkingIdByUserIdList);
        //执行到次说明有parkingId
        if(costStatistic.getStartDate()==null||costStatistic.getEndDate()==null){
            List<ParkingFeeInfo> allParkingFee = parkingFeeInfoDao.getAllParkingFee(parkingIdByUserIdList);
            for (ParkingFeeInfo parkingFee:allParkingFee) {
                //System.out.println(parkingFee.getParkingFee());
                if(parkingFee.getWhether().equals("是")){
                    sumFeed+=Integer.parseInt(parkingFee.getParkingFee().trim());
                }else {
                    sumNotFeed+=Integer.parseInt(parkingFee.getParkingFee().trim());
                }
                sum+=Integer.parseInt(parkingFee.getParkingFee().trim());
            }
        }else {
            //对日期进行遍历
            int yearStart = DateUtil.getYear(costStatistic.getStartDate());
            int monthStart = DateUtil.getMonth(costStatistic.getStartDate());
            int yearEnd = DateUtil.getYear(costStatistic.getEndDate());
            int monthEnd = DateUtil.getMonth(costStatistic.getEndDate());
            for (int i = yearStart;i<=yearEnd;i++){
                int startMonth,endMonth;
                if(i==yearStart&&i==yearEnd){
                    startMonth = monthStart;
                    endMonth = monthEnd;
                }else if(i==yearStart){
                    startMonth = monthStart;
                    endMonth = 12;
                }else if(i==yearEnd){
                    startMonth = 1;
                    endMonth = monthEnd;
                }else {
                    startMonth = 1;
                    endMonth = 12;
                }
                for(int j = startMonth;j<=endMonth;j++){
                    List<ParkingFeeInfo> allParkingFeeByDate = parkingFeeInfoDao.getAllParkingFeeByDate(i, j, parkingIdByUserIdList);
                    for (ParkingFeeInfo parkingFee:allParkingFeeByDate) {
                        if(parkingFee.getWhether().equals("是")){
                            sumFeed+=Integer.parseInt(parkingFee.getParkingFee().trim());
                        }else {
                            sumNotFeed+=Integer.parseInt(parkingFee.getParkingFee().trim());
                        }
                        sum+=Integer.parseInt(parkingFee.getParkingFee().trim());
                    }
                }
            }
        }
        costStatistic1.setParkingFeeValue(String.valueOf(sum),String.valueOf(sumFeed),String.valueOf(sumNotFeed));
        return costStatistic1;
    }

    @Override
    public int remindPayParkingFee() {
        List<Integer> residentIdList = residentDao.getResidentIdList(null, null);//获取全部居民信息
        for (int id:residentIdList) {
            List<Integer> parkingIdByUserId = parkingDao.getParkingIdByUserId(id);//获得停车位id
            for (int parkingId:parkingIdByUserId) {
                List<Integer> parkingNotFeed = parkingFeeInfoDao.getParkingNotFeed(parkingId);
                if(parkingNotFeed.size()>0){
                    FeeStatus feeStatus = feeStatusDao.getFeeStatus(id);
                    System.out.println(feeStatus);
                    if(feeStatus==null){
                        FeeStatus feeStatusNew = new FeeStatus();
                        feeStatusNew.setId(feeStatusDao.getMaxId()+1);
                        feeStatusNew.setResidentId(id);
                        feeStatusNew.setParkingFeeStatus(1);
                        feeStatusNew.setPropertyFeeStatus(0);
                        feeStatusNew.setKeyWord("待缴费");
                        feeStatusDao.addFeeStatus(feeStatusNew);
                    }else {
                        if(feeStatus.getParkingFeeStatus()!=1){
                            feeStatus.setParkingFeeStatus(1);
                            feeStatusDao.updateParkingFeeStatus(feeStatus);
                        }
                    }
                }else {
                    //修改状态0
                    FeeStatus feeStatus = feeStatusDao.getFeeStatus(id);
                    System.out.println(feeStatus);
                    if(feeStatus==null){
                        FeeStatus feeStatusNew = new FeeStatus();
                        feeStatusNew.setId(feeStatusDao.getMaxId()+1);
                        feeStatusNew.setResidentId(id);
                        feeStatusNew.setParkingFeeStatus(0);
                        feeStatusNew.setPropertyFeeStatus(0);
                        feeStatusNew.setKeyWord("待缴费");
                        feeStatusDao.addFeeStatus(feeStatusNew);
                    }else {
                        if(feeStatus.getParkingFeeStatus()!=1){
                            feeStatus.setParkingFeeStatus(0);
                            feeStatusDao.updateParkingFeeStatus(feeStatus);
                        }
                    }
                }
            }
        }
        return 0;
    }
}
